package com.softaria.gwt.common.client.dialog;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.softaria.gwt.common.client.widget.Button;

public class AlertDialog extends Dialog {

  public AlertDialog(String title, String message) {
    this(title, message, "OK");
  }

  public AlertDialog(String title, Throwable th) {
    this(title, "", "Close");

    StringBuilder builder = new StringBuilder();
    builder.append("<b>").append(th.getMessage()).append("</b><br/><br/>");
    for (StackTraceElement element : th.getStackTrace()) {
      builder.append(element).append("<br/>");
    }

    setTextAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    setMessage(builder.toString());
  }

  public AlertDialog(String title, String message, String buttonLbl) {
    super(title, message);

    Button button = new Button(buttonLbl, new ClickHandler() {
      public void onClick(ClickEvent event) {
        hide();
      }
    });
    addButton(button);
  }

}
